package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuCourierStation;
import com.hualan.dao.impl.StationDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/courierStation/addCourierStation")
public class AddCourierStationServlet extends HttpServlet {
	
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StationDaoImpl stationDao = new StationDaoImpl();
        PanghuCourierStation panghuCourierStation=new PanghuCourierStation();
        Map<String,Object> map=new HashMap<>();
        int id=0;
        String name = req.getParameter("courierStationName");
        System.out.println(name);
        String province = req.getParameter("courierStationProvince");
        String city = req.getParameter("courierStationCity");
        String district = req.getParameter("courierStationDistrict");
        String address = req.getParameter("courierStationAddress");
        String communityId = req.getParameter("courierStationCommunityId");
        String image = req.getParameter("courierStationImage");
        if (name==null){
            name="";
        }
       if (province==null){
            province="";
        }
       if (city==null){
            city="";
        }
       if (district==null){
            district="";
        }
       if (address==null){
            address="";
        }
        if (image == null || "".equals(image)) {
            image = "暂无图片信息";
        }
        if (communityId == null || "".equals(communityId)) {

        }else {
            id=Integer.parseInt(communityId);
        }
        panghuCourierStation.setCourierStationName(name);
        panghuCourierStation.setCourierStationProvince(province);
        panghuCourierStation.setCourierStationCity(city);
        panghuCourierStation.setCourierStationDistrict(district);
        panghuCourierStation.setCourierStationAddress(address);
        panghuCourierStation.setCourierStationImage(image);
        panghuCourierStation.setCourierStationCommunityId(id);
        int row=stationDao.insert(panghuCourierStation);
        if (row>0){
            map.put("code",200);
            map.put("message","新增成功");
        }else {
            map.put("code",300);
            map.put("message","新增失败");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }
}
